package com.test.caine.testalarm;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

import java.util.Calendar;

import static android.os.Build.VERSION_CODES.O;

public class MainActivity extends AppCompatActivity {

    private String myChannel = "my_channel";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

       FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NotificationManager manager =
                        (NotificationManager) MainActivity.this.getSystemService(Context.NOTIFICATION_SERVICE);
                pushNotification(manager);
            }
        });

        //设置闹钟时间
        AlarmManager manager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
        Intent intent= new Intent(this,AlarmManager.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this,1,intent,PendingIntent.FLAG_CANCEL_CURRENT);
//        //18点整提醒用户
//        Calendar afternoon = Calendar.getInstance();
//        afternoon.set(Calendar.HOUR_OF_DAY,18);
//        afternoon.set(Calendar.MINUTE,0);
//        afternoon.set(Calendar.SECOND,0);

        //现在
        Calendar current = Calendar.getInstance();

        //6点整提醒用户
        Calendar alarmTime = Calendar.getInstance();
        alarmTime.set(Calendar.HOUR_OF_DAY,6);
        alarmTime.set(Calendar.MINUTE,0);
        alarmTime.set(Calendar.SECOND,0);
        //一天提醒两次，早晚六点
        if (current.after(alarmTime)){
            alarmTime.add(Calendar.HOUR_OF_DAY,12);
        }

        manager.setRepeating(
                AlarmManager.RTC,
                alarmTime.getTimeInMillis(),
                AlarmManager.INTERVAL_HALF_DAY,
                pendingIntent);


    }

    private void pushNotification(NotificationManager manager) {
        //安卓O 新特性 NotificationChannel
        if (Build.VERSION.SDK_INT>=O){
            NotificationChannel mChannel = new NotificationChannel(
                    myChannel,
                    "channel_name",
                    NotificationManager.IMPORTANCE_LOW);
            mChannel.setDescription("channel description");
            mChannel.enableLights(true);
            mChannel.setLightColor(Color.RED);
            mChannel.enableVibration(true);
            mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            manager.createNotificationChannel(mChannel);
            //通知
            PendingIntent pendingIntent = PendingIntent.getActivity(
                    this,
                    1,
                    new Intent(this,LoginActivity.class),
                    PendingIntent.FLAG_UPDATE_CURRENT);
            Notification builder = new Notification.Builder(this,myChannel)
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent)
                    .setSmallIcon(R.drawable.ic_camera)
                    .setContentTitle("记录你的成长:click")
                    .setContentText("每天一张，流逝的没一点青春，我都记录着:click")
                    .build();
            manager.notify(1,builder);
        }else {
            pushNotification();
        }
    }

    private void pushNotification() {
        PendingIntent pendingIntent = PendingIntent.getActivity(
                this,
                1,
                new Intent(this,MainActivity.class),
                PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
                .setSmallIcon(R.drawable.ic_camera)
                .setContentTitle("记录你的成长:click")
                .setContentText("每天一张，流逝的没一点青春，我都记录着:click");
        manager.notify(1,builder.build());
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
